﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.ServiceModel;
using System.Windows.Forms;
using SLS.DataBase;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.Contract;
namespace LanSunERP
{
    // Custom message.

    [MessageContract(IsWrapped = true)]
    [ServiceBehavior(
        InstanceContextMode = InstanceContextMode.PerSession)]
    public partial class ERPBLayerService : IERPBLayerService, IDisposable
    {
        DataTable dtRequest;
        DataSet dsResult;
        SLDBSET setGlobal = new SLDBSET();
        SLQDBSET setQueryCond = new SLQDBSET(); 
        MessageEx BaseMessage = new MessageEx();
        Dictionary<string, object> dicDsParameters;
        Dictionary<string, object> DicGlobalPara; 
        string AppXMLPath = Application.StartupPath.ToString().Trim() + @"\ExXML\";
        string UIXMLPath = "";
        DataTable TBForeignKeyList = null;
        DataRow[] dwForeignKeyLists;
        DataRow dwForeignKeyList;
        SqlConnection LanSunERPCon;
        SqlConnection LanSunERPQueryCondCon;
        public ERPBLayerService()
        {
            try
            {
                string strConn = @"Data Source=SUNLIGHT\LANSUNERP;Initial Catalog=LANSUNERP;Persist Security Info=True;User ID=LanSunERP2010;Password=LanSunERP2010";

                LanSunERPCon = new SqlConnection(strConn);
                strConn = @"Data Source=SUNLIGHT\LANSUNERP;Initial Catalog=LANSUNERPQueryCond;Persist Security Info=True;User ID=LanSunERP2010;Password=LanSunERP2010";
                LanSunERPQueryCondCon = new SqlConnection(strConn);
                TraceConfiguration();

                dicDsParameters = new Dictionary<string, object>();
                DicGlobalPara = new Dictionary<string, object>();
                DicGlobalPara.Add("dsParameters", null);
                this.UIXMLPath = AppXMLPath + "ERPBLDataSet.XML";
                BaseMessage.SM = "9";
                BaseMessage.AgentID = "ERPBLayerService";
                DicGlobalPara.Add("msgRequest", BaseMessage);
                DicGlobalPara.Add("TBForeignKeyList", null);
                DicGlobalPara.Add("LanSunERPCon", LanSunERPCon);
                DicGlobalPara.Add("LanSunERPQueryCondCon", LanSunERPQueryCondCon);
                AgentInitialization(BaseMessage);
                UpdateTheDataSet();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void Dispose()
        {
            dicDsParameters.Clear();
        }
        public MessageEx AskService(MessageEx msg)
        {
            MessageEx msgX = null;
            try
            {
                msgX = new MessageEx(msg); 
                string JobID = msg.JobID;
                string sAgentID = msg.AgentID.ToUpper();

                DicGlobalPara["msgRequest"] = msg; 
                if (dicDsParameters.ContainsKey(sAgentID))
                {
                    DataSet dsTmp = (DataSet)dicDsParameters[sAgentID];
                    DicGlobalPara["dsParameters"] = dsTmp; 
                }
                SQLFunction m_SQLFunction;
                //Tracer.Verbose("AskService", msgRequest);
                switch (JobID)
                {
                    case "OnQueryAgentList":
                        msgX = AgentInitialization(msg); 
                        break;  
                    case "OnSaveRecord":
                        m_SQLFunction = new SQLFunction(DicGlobalPara);
                        string UpdateWhenExist = (string)msg.Tag;
                        bool blUPdate = true;
                        if (UpdateWhenExist == "NotUpdateWhenExist")
                        {
                            blUPdate = false;
                        }
                        msgX.setEvent = m_SQLFunction.SaveRecord(msg.setEvent, blUPdate);
                        break;
                    case "OnDeleteRecord":
                        m_SQLFunction = new SQLFunction(DicGlobalPara);

                        m_SQLFunction.DeleteRecord(msg.setEvent);
                        break; 
                    default:
                        m_SQLFunction = new SQLFunction(DicGlobalPara);
                        msgX.setEvent = m_SQLFunction.QueryAndCreateTable(null);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return msgX;
        }
       
    }
}